<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<!--导航栏-->
		<pageLayout
			:barTitle="title"
			:isSearchNull="isSearchNull"
			:isOpen="isOpen"
			@rightClick="clickSearch">
		</pageLayout>
		<!--查询弹窗-->
		<up-popup :z-index="90" :show="isOpen" @close="closeSearch" mode="top">
			<view @touchmove.stop.prevent>
			<scroll-view class="search" scroll-y="true" @touchmove.stop>
				<view class="search-form">
					<uni-forms ref="searchRef" :modelValue="searchModel" label-width="160rpx">
						<uni-forms-item label="商户池ID">
							<uni-easyinput v-model="searchModel.clueId" placeholder="请输入商户池ID" />
						</uni-forms-item>
						<uni-forms-item label="跟进用户">
							<uni-easyinput v-model="searchModel.followUserName" placeholder="请输入跟进用户" />
						</uni-forms-item>
						<uni-forms-item label="客户姓名">
							<uni-easyinput v-model="searchModel.customerName" placeholder="请输入客户姓名" />
						</uni-forms-item>
						<uni-forms-item label="联系电话">
							<uni-easyinput v-model="searchModel.phone" placeholder="请输入联系电话" />
						</uni-forms-item>
						<uni-forms-item label="门店名称">
							<uni-easyinput v-model="searchModel.storeName" placeholder="请输入门店名称" />
						</uni-forms-item>
					</uni-forms>
					<view class="search-footer main-flex">
						<up-button
							class="search-footer-item"
							text="搜索"
							type="primary"
							@click="searchSubmit"
							icon="search"></up-button>
						<up-button
							class="search-footer-item"
							@click="resetSubmit"
							text="重置"></up-button>
					</view>
				</view>
			</scroll-view>
			</view>
		</up-popup>
		<view class="content">
			<!--操作-->
			<!-- <view class="operate main-flex" v-if="checkPermi(['zhouyz:clueFollowRecord:add'])">
				<view class="operate-item">
					<up-button
						class="operate-btn main-flex"
						size="mini"
						type="primary"
						@click="add()">
						<uni-icons class="operate-btn-icon" type="plusempty" size="28rpx" color="#ffffff"></uni-icons>
						<view class="operate-btn-text">添加</view>
					</up-button>
				</view>
			</view> -->
			<!--内容-->
			<view class="table">
				<ty-table
					:data="tableList"
					:columns="columns"
					:sortable="true"
					@row-click="rowClick"
					:cellClassName="() => 'table-cell'"
					:headerCellClassName="() => 'table-cell'"
					:rowClassName="() => 'table-row'"
					stripe
					:isOperateBtn="(row) => {
						return checkPermi(['zhouyz:salesmanClueFollowProduct:add', 'zhouyz:clueFollowRecord:edit', 'zhouyz:clueFollowRecord:remove'])
					}"
					border>
					<template v-slot:left="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">
								线索ID：
								<text class="table-td-text">{{row.clueId}}</text></view>
							<view class="table-td-row">
								跟进用户：
								<text class="table-td-text">{{row.followUserName}}</text></view>
							<view class="table-td-row">
								类型：
								<uni-tag size="small" :text="arrText(salesmanClueFollowStatusList, row.followType)" /></view>
							<view class="table-td-row">
								创建者：
								<text class="table-td-text">{{row.createName}}</text></view>
							<view class="table-td-row">
								更新者：
								<text class="table-td-text">{{row.updateName}}</text></view>
						</view>
					</template>
					<template v-slot:right="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">
								跟进前状态：
								<text class="table-td-text">{{arrText(salesmanClueFollowLevelList, row.oldLevel)}}</text></view>
							<view class="table-td-row">
								跟进后状态：
								<text class="table-td-text">{{arrText(salesmanClueFollowLevelList, row.newLevel)}}</text></view>
							<view class="table-td-row">
								时间：
								<text class="table-td-text">{{row.createTime}}</text></view>
						</view>
					</template>
					<template v-slot:operate="{ row }">
						<view class="table-td-operate table-cell table-td-row main-flex">
							<view v-if="checkPermi(['zhouyz:salesmanClueFollowProduct:add'])" class="table-operate-btn main-flex">
								<up-button
									size="mini"
									type="primary"
									@click.stop="testSalesmanClueFollowProduct(row)">
									<uni-icons class="table-operate-btn-icon" type="plus" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">到店试品</view>
								</up-button>
							</view>
							<view v-if="checkPermi(['zhouyz:clueFollowRecord:edit'])" class="table-operate-btn main-flex">
								<up-button
									size="mini"
									type="primary"
									@click.stop="add(row.id)">
									<uni-icons class="table-operate-btn-icon" type="compose" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">修改</view>
								</up-button>
							</view>
							<view v-if="checkPermi(['zhouyz:clueFollowRecord:remove'])" class="table-operate-btn main-flex">
								<up-button
									class="table-operate-btn main-flex"
									size="mini"
									type="error"
									@click.stop="deleteFun(row.id)">
									<uni-icons class="table-operate-btn-icon" type="trash" size="20rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">删除</view>
								</up-button>
							</view>
						</view>
					</template>
					<template v-slot:empty>
						<view class="table-empty">
							<up-empty mode="data"/>
						</view>
					</template>
				</ty-table>
			</view>
			<!--分页-->
			<view class="pagination" v-show="total > 0">
				<ty-pagination
					:total="total"
					:pageSize="pageSize"
					:currentPage="pageNum"
					:pageSizes="[10, 20, 30, 50]"
					mode="simple"
					layout="first,prev,page,next,last"
					@sizeChange="sizeChange"
					@change="pageChange">
				</ty-pagination>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad, onPullDownRefresh } from '@dcloudio/uni-app';
import { computed, onMounted, ref, getCurrentInstance } from 'vue'
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { salesmanClueFollow, salesmanClueFollowDel } from "@/api/erp_api/sale"
import { isObjectEmptySimple, copyText, arrText } from '@/utils/ty.js'
import useUserStore from '@/store/modules/user'
import { salesmanClueFollowLevelList, salesmanClueFollowStatusList } from '@/utils/config.js'

	const userStore = useUserStore()
	const { proxy } = getCurrentInstance()
	// 标题设置
	const title = ref('')
	// 确认搜索
	const searchRef = ref(null)
	const searchModel = ref({
		clueId: '',
		followUserName: '',
		customerName: '',
		phone: '',
		storeName: ''
	})
	onLoad((option) => {
		title.value = getTitle(userStore.menu)
		if (option.clueId) {
			searchModel.value.clueId = option.clueId
		}
		uni.setNavigationBarTitle({
			title: title.value 
		})
		uni.$on('refreshList', () => {
			getList()
		})
	})
	
	// 搜索相关
	const isSearchNull = ref(true)
	const isOpen =  ref(false)
	const clickSearch = (val) => {
		isOpen.value = val
	}
	const closeSearch = (val) => {
		isOpen.value = false
	}
	
	const searchSubmit = () => {
		pageNum.value = 1
		tableList.value = []
		isOpen.value = false
		searchFirst()
	}
	// 重置搜索
	const resetSubmit = () => {
		// searchRef.value.resetFields()
		searchModel.value = {
			clueId: '',
			followUserName: '',
			customerName: '',
			phone: '',
			storeName: ''
		}
		isSearchNull.value = true
		isOpen.value = false
		searchFirst()
	}
	
	
	// 数据查询
	const pageNum = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const tableList = ref([])
	const isLoading = ref(false)
	
	// 表格头部
	const columns = ref([
		{ title: '跟进用户', key: 'left', width: '220rpx' },
		{ title: '跟进内容', key: 'content' },
		{ title: '跟进状态', key: 'right' }
	])
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		tableList.value = []
		isSearchNull.value = isObjectEmptySimple(searchModel.value)
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value
		}
		if (searchModel.value.clueId) {
			params.clueId = searchModel.value.clueId
		}
		if (searchModel.value.followUserName) {
			params.followUserName = searchModel.value.followUserName
		}
		if (searchModel.value.customerName) {
			params.customerName = searchModel.value.customerName
		}
		if (searchModel.value.phone) {
			params.phone = searchModel.value.phone
		}
		if (searchModel.value.storeName) {
			params.storeName = searchModel.value.storeName
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		salesmanClueFollow(params).then(res => {
			tableList.value = res.rows || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// 详情
	const rowClick = (row) => {
		uni.navigateTo({
			url: `/pages/sale/salesmanClueFollowDetail?id=${row.id}`
		})
	}
	
	//编辑
	const add = (id) => {
		uni.navigateTo({
			url: id ? `/pages/sale/salesmanClueFollowAdd?id=${id}` : '/pages/sale/salesmanClueFollowAdd'
		})
	}
	
	// 试品
	const testSalesmanClueFollowProduct = (row) => {
		uni.navigateTo({
			url: `/pages/sale/salesmanClueFollowProductTest?clueId=${row.clueId}&id=${row.id}`
		})
	}
	
	//删除
	const deleteFun = (id) => {
		uni.showModal({
			title: '删除',
			content: `是否确认删除线索跟进编号为"${id}"的数据项?`,
			success: function (res) {
				if (res.confirm) {
					deleteOne(id)
				}
			}
		})
	}
	const deleteOne = (id) => {
		salesmanClueFollowDel({
			id
		}).then(res => {
			modal.showToast('删除成功')
			getList()
		}).catch((err) => {
			modal.showToast(err)
		})
	}
	
	// 下拉刷新
	onPullDownRefresh(() => {
		getList()
	})
	onMounted(() => {
		searchFirst()
	})
</script>

<style scoped lang="scss">
</style>
